<?php
action_gatekeeper('deleteproject');
$project = get_entity($vars['project_guid']);
if ($project) {

//    $metadata_options = array(
//        array(
//            'name' => 'group_project',
//            'value' => $project->group_project
//        ),
//        array(
//            'name' => 'semester',
//            'value' => $project->semester
//        ),
//        array(
//            'name' => 'academic_year',
//            'value' => $project->academic_year
//        )
//    );
//
//    $options = array(
//        'type' => "object",
//        'subtype' => 'project',
//        'container_guid' => $vars ['group_guid'],
//        'metadata_name_value_pairs' => $metadata_options
//    );
//
//    $projectlist = elgg_get_entities_from_metadata($options);
//    $count = 0;
//    foreach ($projectlist as $project){
//        $count++;
//    }
//
//    system_message((string)$count);
//
//    if ($count == 1) {
//        $projectlists = elgg_get_entities(array(
//            'type' => "object",
//            'subtype' => "projectlist",
//            'container_guid' => $vars ['group_guid']
//        ));
//
//        foreach ($projectlists as $projectlist) {
//            $ar_groupproject = explode(",", $projectlist->group_project);
//            $ar_deadline_editted = explode(",", $projectlist->deadline_editted);
//            $ar_deadline = explode(",", $projectlist->deadline);
//            $ar_semester = explode(",", $projectlist->semester);
//            $ar_academic = explode(",", $projectlist->academic_year);
//
//            $found = -1;
//            for ($i = 0; $i < count($ar_groupproject); $i++) {
//                $old_semester = $ar_semesters[$i];
//                $old_academic_year = $ar_academic_years[$i];
//                if ($ar_groupproject [$i] == $group_project && $old_semester == $project->semester && $old_academic_year == $project->academic_year) {
//                    $found = $i;
//                    break;
//                }
//            }
//
//            if ($found != -1){
//                $str = $ar_groupproject[0];
//                for ($i = 1; $i < count($ar_groupproject); $i++) {
//                    $str .= "," . $ar_groupproject [$i];
//                }
//
//                $projectlist->group_project = $str;
//
//                $str = $ar_deadline[0];
//                for ($i = 1; $i < count($ar_deadline); $i++) {
//                    $str .= "," . $ar_deadline [$i];
//                }
//
//                $projectlist->deadline = $str;
//
//                $str = $ar_semester[0];
//                for ($i = 1; $i < count($ar_semester); $i++) {
//                    $str .= "," . $ar_semester [$i];
//                }
//
//                $projectlist->semester = $str;
//
//                $str = $ar_deadline_editted[0];
//                for ($i = 1; $i < count($ar_deadline_editted); $i++) {
//                    $str .= "," . $ar_deadline_editted [$i];
//                }
//
//                $projectlist->deadline_editted = $str;
//
//                $str = $ar_academic[0];
//                for ($i = 1; $i < count($ar_academic); $i++) {
//                    $str .= "," . $ar_academic [$i];
//                }
//
//                $projectlist->academic_year = $str;
//
//                $projectlist->save();
//            }
//            break;
//        }
//    }

    $project->delete(true);
    system_message(elgg_echo('submana:message:deletedproject'));
} else {
    register_error(elgg_echo('submana:message:failded'));
}

forward(REFERER);